package gui;
import javax.swing.*;

import database.CustData;
import database.OracleConnection;

import java.awt.*;
import java.awt.event.*;


public class RegistrationWindow extends JDialog implements ActionListener
{
	// MvbOracleConnection represents a connection to an Oracle database
	private OracleConnection mvb = OracleConnection.getInstance();



	// components of the registration window
	private JTextField usernameField = new JTextField(15);
	private JPasswordField passwordField = new JPasswordField(15);	  
	private JTextField nameField = new JTextField(15);
	private JTextField addressField = new JTextField(15);
	private JTextField phoneField = new JTextField(15);
	private JLabel usernameLabel = new JLabel("Enter username:  ");
	private JLabel passwordLabel = new JLabel("Enter password:  ");
	private JLabel nameLabel = new JLabel("Enter name:  ");
	private JLabel addressLabel = new JLabel("Enter address:  ");
	private JLabel phoneLabel = new JLabel("Enter phone number:  ");
	private JButton registerButton = new JButton("Register");


	/*
	 * Default constructor. The registration window is constructed here.
	 */
	public RegistrationWindow(JFrame parent)
	{
		super(parent, "User Registration", true);
		setResizable(false);

		passwordField.setEchoChar('*');

		// content pane for the registration window
		JPanel registerPane = new JPanel();
		setContentPane(registerPane);


		/*
		 * layout components using the GridBag layout manager
		 */ 

		GridBagLayout gb = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();

		registerPane.setLayout(gb);
		registerPane.setBorder(BorderFactory.createEmptyBorder(22, 12, 11, 11));

		// place the username label 
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 10, 0, 0);
		gb.setConstraints(usernameLabel, c);
		registerPane.add(usernameLabel);

		// place the text field for the username 
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 0, 10);
		gb.setConstraints(usernameField, c);
		registerPane.add(usernameField);

		// place password label
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 10, 0, 0);
		gb.setConstraints(passwordLabel, c);
		registerPane.add(passwordLabel);

		// place the password field 
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 0, 10);
		gb.setConstraints(passwordField, c);
		registerPane.add(passwordField);

		// place name label
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 10, 0, 0);
		gb.setConstraints(nameLabel, c);
		registerPane.add(nameLabel);

		// place the name field 
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 0, 10);
		gb.setConstraints(nameField, c);
		registerPane.add(nameField);
		
		// place address label
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 10, 0, 0);
		gb.setConstraints(addressLabel, c);
		registerPane.add(addressLabel);

		// place the address field 
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 0, 10);
		gb.setConstraints(addressField, c);
		registerPane.add(addressField);
		
		// place phone label
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 10, 0, 0);
		gb.setConstraints(phoneLabel, c);
		registerPane.add(phoneLabel);

		// place the phone field 
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 0, 10);
		gb.setConstraints(phoneField, c);
		registerPane.add(phoneField);
		
		// place the register button
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 10, 5, 10);
		c.anchor = GridBagConstraints.CENTER;
		gb.setConstraints(registerButton, c);
		registerPane.add(registerButton);

		// end of layout

		// Register password field and OK button with action event handler.
		// An action event is generated when the return key is pressed while 
		// the cursor is in the password field or when the OK button is pressed.
		registerButton.addActionListener(this);

		// anonymous inner class for closing the window
		addWindowListener(new WindowAdapter() 
		{
			public void windowClosing(WindowEvent e) 
			{ 
				//System.exit(0); 
			}
		});

		// initially, place the cursor in the username text field
		usernameField.requestFocus();	

		// auto-login:
		mvb.connect("ora_o4b8", String.valueOf("a29458957"));

	}


	/*
	 * event handler register button
	 */ 	    
	public void actionPerformed(ActionEvent e) 
	{
		CustData cd = new CustData();
		if (cd.isUser(Integer.valueOf(usernameField.getText().trim())))
		{
			usernameField.setText("Pick Username");
		}
		else if((usernameField.getText().trim().length() != 0) && (String.valueOf(passwordField.getPassword())!=null) && (nameField.getText().trim().length() != 0) &&
								(addressField.getText().trim().length() != 0) && (phoneField.getText().trim().length() != 0))
		{
			cd.registerCustomer(Integer.valueOf(usernameField.getText().trim()), String.valueOf(passwordField.getPassword()), nameField.getText(),
								addressField.getText(), phoneField.getText());
			dispose();
			//loginAttempts++;

			//if (loginAttempts >= 3)
			//{
			//	dispose();
			//	System.exit(0);
		//	}
		//	else
		//	{
		//		// clear the password
		//	passwordField.setText("");
		//	}
		}  
	}     
}

